package com.shchekoldin.juliet.packager.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class FileContentReader {

    private final int BUFFER_SIZE = 10240;

    private static FileContentReader instance = null;

    // -------------------------------------------------------
    // -                      INSTANCE                       -
    // -------------------------------------------------------

    public static FileContentReader getInstance() {
        if (instance == null) instance = new FileContentReader();
        return instance;
    }

    private FileContentReader() { }

    // -------------------------------------------------------
    // -                        LOGIC                        -
    // -------------------------------------------------------

    //
    // API
    //

    public String run(String filePath) throws IOException {
        StringBuilder result = new StringBuilder();
        Reader fileReader = new InputStreamReader(new FileInputStream(filePath), "utf-8");

        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead = fileReader.read(buffer);

        while (bytesRead != -1) {
            result.append(buffer, 0, bytesRead);
            bytesRead = fileReader.read(buffer);
        }

        return result.toString();
    }

}
